package com.ks.network.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.constant.DatePanel;
import com.ks.core.dto.OperateBase;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.core.util.ToolUtil;
import com.ks.network.entity.BNetworkBase;
import com.ks.network.dao.BNetworkBaseMapper;
import com.ks.network.entity.BNetworkUserCount;
import com.ks.network.service.IBNetworkBaseService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ks.network.service._ServiceBase;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务网点-网点基本信息表 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-21
 */
@Service
public class BNetworkBaseServiceImpl extends _ServiceBase<BNetworkBaseMapper, BNetworkBase> implements IBNetworkBaseService {

    @Resource
    private BNetworkBaseMapper bNetworkBaseMapper;

    @Override
    public Map<String, Object> getBNetworkBaseUserCount(OperationContext context) {
        Map<String, Object> map = new HashMap<>();
        EntityWrapper<BNetworkBase> wrapper = new EntityWrapper<>();
        BNetworkBase bNetworkBase = new BNetworkBase();
        bNetworkBase.setDelMark(Const.DEL_FLAG_NORMAL);
        bNetworkBase.setTenantID(context.getTenantId().intValue());
        wrapper.setEntity(bNetworkBase);
        Integer integer = bNetworkBaseMapper.selectCount(wrapper);
        map.put("CardName", "网点服务数");
        map.put("DataCount", "1");
        List list = new ArrayList();
        DatePanel panel = new DatePanel();
        panel.setDataTime(new Date());
        panel.setDataValue(integer);
        panel.setValueType("STR");
        list.add(panel);
        map.put("date", list);
        return map;
    }

    @Override
    public Map<String, Object> getBNetworkMapUserCount(OperationContext context) {
        List<BNetworkUserCount> bNetworkMapUserCount = bNetworkBaseMapper.getBNetworkMapUserCount(context.getTenantId().intValue());
        Map<String, Object> map = new HashMap<>();
        map.put("CardName", "服务网点地图");
        map.put("DataCount", "1");
        List list = new ArrayList();
        if (ToolUtil.isNotEmpty(bNetworkMapUserCount)) {
            for (int i = 0; i < bNetworkMapUserCount.size(); i++) {
                DatePanel panel = new DatePanel();
                panel.setDataName(bNetworkMapUserCount.get(i).getIProvince());
                panel.setDataTime(new Date());
                panel.setDataValue(0);
                panel.setValueType("STR");
                list.add(panel);
            }
        }
        map.put("date", list);
        return map;
    }

    @Override
    public int addNetWork(OperationContext context, BNetworkBase bNetworkBase) {
        if (!isSystem(context)) {
            bNetworkBase.setCreateUserID(context.getUserId().intValue());
            bNetworkBase.setOrgID(context.getOrgId().intValue());
            bNetworkBase.setOrgSubID(context.getOrgSubId().intValue());
            bNetworkBase.setTenantID(context.getTenantId().intValue());
        }
        Integer i = bNetworkBaseMapper.insert(bNetworkBase);
        return i;
    }

    @Override
    public int updateNetWork(OperationContext context, BNetworkBase bNetworkBase) {
        Integer i = bNetworkBaseMapper.updateById(bNetworkBase);
        return i;
    }

    @Override
    public Map<String, Object> selectNetWork(OperationContext context, BNetworkBase bNetworkBase) {
        Map<String, Object> objectMap = new HashMap<>();
        bNetworkBase = selectById(bNetworkBase);
        objectMap.put("data", bNetworkBase);
        return objectMap;
    }

    @Override
    public Map<String, Object> selectNetWorkList(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<BNetworkBase> wrapper = new EntityWrapper<>();
        BNetworkBase networkBase = new BNetworkBase();
        if (ToolUtil.isNotEmpty(fields)) {
            for (SearchCriteriaBase.matchField field : fields) {
                if ("area".equals(field.getName())) {
                    if (ToolUtil.isNotEmpty(field.getValue())) {
                        String[] areas = field.getValue().split(",");
                        for (int i = 0; i < areas.length; i++) {
                            if (i == 0) {
                                networkBase.setIProvinceId(Integer.valueOf(areas[i]));
                            } else if (i == 1) {
                                networkBase.setICityId(Integer.valueOf(areas[i]));
                            } else if (i == 2) {
                                networkBase.setIAreaId(Integer.valueOf(areas[i]));
                            }

                        }
                    }
                }
                if ("name".equals(field.getName())) {
                    if (ToolUtil.isNotEmpty(field.getValue())) {
                        wrapper.like("iNetName", field.getValue());
                    }
                }
                if ("state".equals(field.getName())) {
                    if (ToolUtil.isNotEmpty(field.getValue())) {
                        networkBase.setSState(Integer.valueOf(field.getValue()));
                    }
                }
                if ("tenantID".equals(field.getName())) {
                    if (ToolUtil.isNotEmpty(field.getValue())) {
                        networkBase.setTenantID(Integer.valueOf(field.getValue()));
                    }
                }
                if ("createTime".equals(field.getName())) {
                    if (ToolUtil.isNotEmpty(field.getValue())) {
                        String[] time = field.getValue().split(",");
                        String filterSql = "createTime >='" + time[0] + " 00:00:00"
                                + "' and createTime <='" + time[1] + " 23:59:59'";
                        wrapper.addFilter(filterSql);
                    }
                }
            }
        }
        networkBase.setDelMark(Const.DEL_FLAG_NORMAL);
        if (!isSystem(context)) {
            networkBase.setTenantID(context.getTenantId().intValue());
        }
        wrapper.setEntity(networkBase);
//        if(searchCriteriaBase.getStartTime()!=null&&searchCriteriaBase.getEndTime()!=null){
//            String filterSql="createTime> '"+searchCriteriaBase.getStartTime()
//                    +"' and createTime<'"+searchCriteriaBase.getEndTime()+"'";
//            wrapper.addFilter(filterSql);
//        }
        Page limit = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        List<BNetworkBase> bNetworkBases = bNetworkBaseMapper.selectPage(limit, wrapper);
        Map<String, Object> resultMap = this.isResultMap(bNetworkBases, searchCriteriaBase);
        Integer count = bNetworkBaseMapper.selectCount(wrapper);
        resultMap.put("count", count);
        return resultMap;
    }

    @Override
    public int deleteNetWork(OperationContext context, OperateBase operateBase) {
        int count = 0;
        ArrayList<String> idList = operateBase.getIdList();
        for (String id : idList) {
            BNetworkBase bNetworkBase = bNetworkBaseMapper.selectById(id);
            bNetworkBase.setDelMark(Const.DEL_FLAG_DEL);
            Integer i = bNetworkBaseMapper.updateById(bNetworkBase);
            count += i;
        }
        return count;
    }


    public Map<String, Object> isResultMap(List list, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        map.put("count", list.size());
        map.put("limit", searchCriteriaBase.getLimit());
        map.put("pageIndex", searchCriteriaBase.getPageIndex());
        map.put("hasMore", false);
        map.put("items", list);
        return map;
    }

    /**********************************************************************************************************/

    /*
    //服务网点-网点列表查询接口
    @Override
    public Map<String, Object> selectNetWork(OperationContext operationContext,SearchCriteriaBase searchCriteriaBase) {
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<BNetworkBase> wrapper = new EntityWrapper<>();
        BNetworkBase networkBase = new BNetworkBase();
        for (SearchCriteriaBase.matchField field : fields) {
            if ("iCity".equals(field.getName())) {
                networkBase.setICity(field.getValue());
            }else if("iArea".equals(field.getName())){
                networkBase.setIArea(field.getValue());
            }
        }
        networkBase.setDelMark(Const.DEL_FLAG_NORMAL);
        networkBase.setOrgID(operationContext.getOrgId().intValue());
        networkBase.setOrgSubID(operationContext.getOrgSubId().intValue());
        networkBase.setTenantID(operationContext.getTenantId().intValue());
        wrapper.setEntity(networkBase);
         if(searchCriteriaBase.getStartTime()!=null&&searchCriteriaBase.getEndTime()!=null){
            String filterSql="createTime> '"+searchCriteriaBase.getStartTime()
                    +"' and createTime<'"+searchCriteriaBase.getEndTime()+"'";
            wrapper.addFilter(filterSql);
        }
        List<BNetworkBase> bNetworkBases = bNetworkBaseMapper.selectList(wrapper);
        if(ToolUtil.isNotEmpty(bNetworkBases)){
            Map<String, Object> resultMap = this.isResultMap(bNetworkBases, searchCriteriaBase);
            return resultMap;
        }
        return null;
    }


    // 服务网点-网点详情接口
    public BNetworkBase selectNetworkDetails(OperationContext operationContext,String iNetName) {
        BNetworkBase bNetworkBase = new BNetworkBase();
        bNetworkBase.setINetName(iNetName);
        networkBase.setOrgID(operationContext.getOrgId().intValue());
        networkBase.setOrgSubID(operationContext.getOrgSubId().intValue());
        networkBase.setTenantID(operationContext.getTenantId().intValue());
        BNetworkBase networkBase = bNetworkBaseMapper.selectOne(bNetworkBase);
        if(ToolUtil.isNotEmpty(networkBase)){
            return networkBase;
        }else
            return null;
    }
*/
}
